﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Data;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TWZ12
    {
        public TWZ12()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();

        }
         
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
            if (!IsTPMode)
                return;
            if (dwMaster == null)
                return;
            TPMode();
        }
        bool IsTPMode = false;
        public void TPMode()
        {
            if (TP == 0)
            {
                MessageBox.Show("请给出批号");
                return;
            }
            if (FOL == 0)
            {
                MessageBox.Show("请给出起始序号");
                return;
            }
            dwMaster["WBBTP"] = Helpers.Helper.Right("0000" + TP.ToString(), 4); 
            dwMaster["WBBFOLGE"] = Helpers.Helper.Right("00" + FOL.ToString(), 2);
            FOL++;
            TMV.UWB18(dwMaster);
        }
       
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            base.NewCommand(sender, e);
            if (!IsTPMode)
            {
                IsTPMode = true;
                vwMaster.View.MoveCurrentTo(-1); 
            } 
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            IsTPMode = false;
            TP = 0.0;
            FOL = 0.0;
            base.SaveCommand(sender, e);
        }

        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear(); 
            if (EXAUF.Length < 14)
                return;
            string wdsa = EXAUF.Substring(0, 1);
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
             
            dwData["WBBAUFTYP"] = "V";
            dwData["WBBAUFKRS"] = EXAUF.Substring(0, 2);
            dwData["WBBAUFLFD"] = EXAUF.Substring(2, 6);
            dwData["WBBAUFPOS"] = EXAUF.Substring(8, 3);
            SResult = TMV.QWB29(dwData);
            MergeData(SResult, true);
            base.OnEXAUFChg(vlaue);
        } 
        public override void OnWZChg(bool value)
        {
            if (!IsWZChg)
                return;
            string exauf = EXAUF;
            EXAUF = "";
            EXAUF = exauf;
            base.OnWZChg(value);
        }
        private void WBB62Command(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                {
                    MessageBox.Show("请先选择任意一个布卷");
                    return;
                }
                
                WBROWS = new List<DataRow>();
                DataTable TB = TBMain.Clone();
                foreach (var item in dlgMaster.SelectedItems)
                {
                    if (!item.GetType().ToString().Contains("DataRowView")) continue;
                    DataRow _dwWB = ((System.Data.DataRowView)item).Row;

                    if (_dwWB["WBBURLIEFSCH"].ToString() != "99999999" && _dwWB["WBBURLIEFSCH"].ToString() != "00000000")
                    {
                        MessageBox.Show("请先取消" + dwMaster["WBBSTKNR"].ToString() + "发货批次");
                        break;
                    }
                    WBROWS.Add(_dwWB);
                }

                if (WBROWS.Count == 0)
                    return;
                TMV.AB05WBFREE(false);
                string exauf = EXAUF;
                EXAUF = "";
                EXAUF = exauf;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void TV_LostFocus(object sender, RoutedEventArgs e)
        {
            //SaveCommand(null, null);
        }
    }
}
